package node.block.stmt;

import midcode.MidCode;
import midcode.MidcodeGenerator;
import midcode.OPType;
import node.Node;
import node.exp.Cond;

public class WhileStmt implements Node{
    private Node whileStmt;
    private Cond cond;
    private String label1;
    private String label2;
    private String label3;

    public WhileStmt(Node whileStmt, Cond cond) {
        this.cond = cond;
        this.whileStmt = whileStmt;
        cond.setFather(this);
    }

    public String getLabel1() {
        return label1;
    }

    public String getLabel2() {
        return label2;
    }

    public String getLabel3() {
        return label3;
    }

    @Override
    public Node getFather() {
        return father;
    }

    @Override
    public String genMidCode() {
        MidcodeGenerator mg = MidcodeGenerator.getInstance();
        label1 = mg.genLabel() + "_while_start";
        label2 = mg.genLabel() + "_while_body";
        label3 = mg.genLabel() + "_while_end";
        mg.addMidcode(new MidCode(OPType.LABEL_GEN, null, null, label1));
        cond.genMidCode();
        mg.addMidcode(new MidCode(OPType.LABEL_GEN, null, null, label2));
        whileStmt.genMidCode();
        cond.genMidCode();
        mg.addMidcode(new MidCode(OPType.LABEL_GEN, null, null, label3));
        return "";
    }
}

